#include <sys_config.h>
#include <types.h>
#include <basic_types.h>
#include <mediatypes.h>
#include <api/libc/printf.h>
#include <api/libc/string.h>
#include <osal/osal.h>

#include <api/libosd/osd_lib.h>

#include "osdobjs_def.h"

#include "string.id"
#include "images.id"
#include "osd_config.h"
#include "osd_rsc.h"

#include "copper_common/system_data.h"
#include "copper_common/menu_api.h"
#include "win_com_menu_define.h"
#include "win_com.h"
#include "win_com_popup.h"

#include "time_zone_name.h"

#define TIME_ZONE(_hoff, _moff) (UINT8)(((int)(_hoff)*2+23)+(int)(_moff)/30)

char win_time_pat_date[] = "d02";
char win_time_pat_time[] = "t1";

/*
REGION_PARAM_T region_param[] = 
{
	{ RS_REGION_TW, TIME_ZONE(8, 0), "TWN" },
//#if(AOV_THAI_SUPPORT_ENABLE == SYS_FUNC_ON)
	{ RS_REGION_THAI, TIME_ZONE(7, 0), "TAI" },
//#endif
//#if(AOV_VIE_SUPPORT_ENABLE == SYS_FUNC_ON)
	{ RS_REGION_VNM, TIME_ZONE(7, 0), "VIE" },
//#endif
//#if(AOV_MALAY_SUPPORT_ENABLE == SYS_FUNC_ON)
	{ RS_REGION_MYS, TIME_ZONE(8, 0), "MAY" },
//#endif
//#if(AOV_IND_SUPPORT_ENABLE == SYS_FUNC_ON)
	{ RS_REGION_IDN, TIME_ZONE(7, 0), "IND" },
//#endif
	{ RS_REGION_FRA, TIME_ZONE(1, 0), "FRA" },
	{ RS_REGION_GER, TIME_ZONE(1, 0), "DEU" },//DEU,not GER(maybe also GER)
	{ RS_REGION_SPA, TIME_ZONE(1, 0), "SPA" },
	{ RS_REGION_ITA, TIME_ZONE(1, 0), "ITA" },
	{ RS_REGION_RUS_KAL, TIME_ZONE(2, 0), "RUS_KAL" },//Martin@20150515
	{ RS_REGION_RUS_MOS, TIME_ZONE(3, 0), "RUS_MOS" },
	{ RS_REGION_RUS_SAM, TIME_ZONE(4, 0), "RUS_SAM" },
	{ RS_REGION_RUS_YEK, TIME_ZONE(5, 0), "RUS_YEK" },
	{ RS_REGION_RUS_OMS, TIME_ZONE(6, 0), "RUS_OMS" },
	{ RS_REGION_RUS_KRA, TIME_ZONE(7, 0), "RUS_KRA" },
	{ RS_REGION_RUS_IRK, TIME_ZONE(8, 0), "RUS_IRK" },
	{ RS_REGION_RUS_YAK, TIME_ZONE(9, 0), "RUS_YAK" },
	{ RS_REGION_RUS_VLA, TIME_ZONE(10, 0), "RUS_VLA" },
	{ RS_REGION_RUS_SRE, TIME_ZONE(11, 0), "RUS_SRE" },
	{ RS_REGION_RUS_KAM, TIME_ZONE(12, 0), "RUS_KAM" },
	{ RS_EGYPT, TIME_ZONE(2, 0), "EGT" },//20	
};
UINT16 region_ids[] =
{
	RS_REGION_TW,//0
//#if(AOV_THAI_SUPPORT_ENABLE == SYS_FUNC_ON)
	RS_REGION_THAI,//1
//#endif
//#if(AOV_VIE_SUPPORT_ENABLE == SYS_FUNC_ON)
	RS_REGION_VNM,//2
//#endif
//#if(AOV_MALAY_SUPPORT_ENABLE == SYS_FUNC_ON)
	RS_REGION_MYS,//3
//#endif
//#if(AOV_IND_SUPPORT_ENABLE == SYS_FUNC_ON)
	RS_REGION_IDN,//4
//#endif
	RS_REGION_FRA,//5
	RS_REGION_GER,//6
	RS_REGION_SPA,//7
	RS_REGION_ITA,//8
	RS_REGION_RUS_KAL,//Martin@20150515
	RS_REGION_RUS_MOS,//10
	RS_REGION_RUS_SAM,//11
	RS_REGION_RUS_YEK,//12
	RS_REGION_RUS_OMS,//13
	RS_REGION_RUS_KRA,//14
	RS_REGION_RUS_IRK,//15
	RS_REGION_RUS_YAK,//16
	RS_REGION_RUS_VLA,//17
	RS_REGION_RUS_SRE,//18
	RS_REGION_RUS_KAM,//19
  RS_EGYPT,//20
};
*/
REGION_PARAM_T region_param[] = 
{
	{ RS_REGION_TW, TIME_ZONE(8, 0), "TWN" },    //0
	{ RS_REGION_THAI, TIME_ZONE(7, 0), "TAI" },  //1
	{ RS_REGION_VNM, TIME_ZONE(7, 0), "VIE" },   //2
	{ RS_REGION_MYS, TIME_ZONE(8, 0), "MAY" },   //3
	{ RS_REGION_IDN, TIME_ZONE(7, 0), "IND" },   //4
	{ RS_EGYPT, TIME_ZONE(2, 0), "EGT" },        //5
	{ RS_REGION_RUS, TIME_ZONE(4, 0), "RUS" },        //6	
	{ RS_UKRAINIAN, TIME_ZONE(4, 0), "UKR" },        //7		
};
UINT16 region_ids[] =
{
	RS_REGION_TW,  //0
	RS_REGION_THAI, //1
	RS_REGION_VNM,  //2
	RS_REGION_MYS,  //3
	RS_REGION_IDN,  //4
  RS_EGYPT,       //5
  RS_REGION_RUS,  //6
  RS_UKRAINIAN,  //7  
};

#define REGION_CNT  ARRAY_SIZE(region_param)

UINT16 get_region_cnt(void)
{
	return REGION_CNT;
}

#ifndef CI_PLUS_PVR_SUPPORT
//#define TIME_DISPLAY 
/*******************************************************************************
*	Objects definition
*******************************************************************************/
extern CONTAINER win_time_con;

extern CONTAINER time_item_con1;
//extern CONTAINER time_item_con2;
extern CONTAINER time_item_con3;
//extern CONTAINER time_item_con4;
extern CONTAINER time_item_con5;
extern CONTAINER time_item_con6;
extern CONTAINER time_item_con7;

extern TEXT_FIELD time_item_txtname1;
//extern TEXT_FIELD time_item_txtname2;
extern TEXT_FIELD time_item_txtname3;
//extern TEXT_FIELD time_item_txtname4;
extern TEXT_FIELD time_item_txtname5;
extern TEXT_FIELD time_item_txtname6;
extern TEXT_FIELD time_item_txtname7;

extern TEXT_FIELD time_item_txtset1;
//extern TEXT_FIELD time_item_txtset2;
extern TEXT_FIELD time_item_txtset3;
//extern TEXT_FIELD time_item_txtset4;
extern EDIT_FIELD time_item_edit5;
extern EDIT_FIELD time_item_edit6;
extern TEXT_FIELD time_item_txtset7;

extern TEXT_FIELD time_item_line1;
//extern TEXT_FIELD time_item_line2;
extern TEXT_FIELD time_item_line3;
//extern TEXT_FIELD time_item_line4;
extern TEXT_FIELD time_item_line5;
extern TEXT_FIELD time_item_line6;
extern TEXT_FIELD time_item_line7;

extern MULTI_TEXT time_mtxt_zone_city;

static VACTION time_item_con_keymap(POBJECT_HEAD pObj, UINT32 key);
static PRESULT time_item_con_callback(POBJECT_HEAD pObj, VEVENT event, UINT32 param1, UINT32 param2);

static VACTION time_con_keymap(POBJECT_HEAD pObj, UINT32 key);
static PRESULT time_con_callback(POBJECT_HEAD pObj, VEVENT event, UINT32 param1, UINT32 param2);
VACTION win_time_edit_keymap(POBJECT_HEAD pObj, UINT32 Key);

#ifndef SD_UI

#define MTXT_SH_IDX	WSTL_TEXT_09_HD
#define MTXT_L	(W_L + 30)
#if Maker_GUI_ON
#ifdef TIME_DISPLAY
#define MTXT_T ( CON_T + (CON_H + CON_GAP)*7 + 8) - 50
#else
#define MTXT_T ( CON_T + (CON_H + CON_GAP)*6 + 8) - 50
#endif
#else
#ifdef TIME_DISPLAY
#define MTXT_T ( CON_T + (CON_H + CON_GAP)*7 + 8)
#else
#define MTXT_T ( CON_T + (CON_H + CON_GAP)*6 + 8)
#endif
#endif
#define MTXT_W (W_W - 60)
#if Maker_GUI_ON
#define MTXT_H 130
#else
#define MTXT_H 150
#endif

#define MENU_TIMESET_GMT_OFFESET_POPUP_H	300

#else

#define CON_GAP 4

#define MTXT_SH_IDX	WSTL_TEXT_09_HD
#define MTXT_L	(W_L + 30)
#ifdef TIME_DISPLAY
#define MTXT_T ( CON_T + (CON_H + CON_GAP)*7 + 5)
#else
#define MTXT_T ( CON_T + (CON_H + CON_GAP)*6 + 5)
#endif
#define MTXT_W (W_W - 60)
#define MTXT_H 150

#define MENU_TIMESET_GMT_OFFESET_POPUP_H	300

#endif


#define LDEF_CON(root, varCon,nxtObj,ID,IDl,IDr,IDu,IDd,l,t,w,h,conobj,focusID)		\
    DEF_CONTAINER(varCon,root,nxtObj,C_ATTR_ACTIVE,0, \
    ID,IDl,IDr,IDu,IDd, l,t,w,h, CON_SH_IDX,CON_HL_IDX,CON_SL_IDX,CON_GRY_IDX,   \
    time_item_con_keymap,time_item_con_callback,  \
    conobj, focusID,1)

#define LDEF_TXTNAME(root,varTxt,nxtObj,ID,IDl,IDr,IDu,IDd,l,t,w,h,resID,str)		\
    DEF_TEXTFIELD(varTxt,root,nxtObj,C_ATTR_ACTIVE,0, \
    ID,IDl,IDr,IDu,IDd, l,t,w,h, TXTN_SH_IDX,TXTN_HL_IDX,TXTN_SL_IDX,TXTN_GRY_IDX,   \
    NULL,NULL,  \
    C_ALIGN_LEFT | C_ALIGN_VCENTER, 0,0,resID,str)

#define LDEF_TXTSET(root,varTxt,nxtObj,ID,IDl,IDr,IDu,IDd,l,t,w,h,resID,str)		\
    DEF_TEXTFIELD(varTxt,root,nxtObj,C_ATTR_ACTIVE,0, \
    ID,IDl,IDr,IDu,IDd, l,t,w,h, TXTS_SH_IDX,TXTS_HL_IDX,TXTS_SL_IDX,TXTS_GRY_IDX,   \
    NULL,NULL,  \
    C_ALIGN_CENTER| C_ALIGN_VCENTER, 20,0,resID,str)

#define LDEF_EDIT(root, varNum, nxtObj, ID, IDl, IDr, IDu, IDd, l, t, w, h,style,cursormode,pat,pre,sub,str)	\
    DEF_EDITFIELD(varNum,root,nxtObj,C_ATTR_ACTIVE,0, \
    ID,IDl,IDr,IDu,IDd, l,t,w,h, TXTS_SH_IDX,WSTL_BUTTON_05_HD,TXTS_SL_IDX,TXTS_GRY_IDX,   \
    win_time_edit_keymap,NULL,  \
    C_ALIGN_CENTER | C_ALIGN_VCENTER, 20,0,style,pat,10,cursormode,pre,sub,str)


#define LDEF_MM_ITEM(root,varCon,nxtObj,varTxtName,varTxtset,varLine,ID,IDu,IDd,l,t,w,h,nameID,setstr)	\
	LDEF_CON(&root,varCon,nxtObj,ID,ID,ID,IDu,IDd,l,t,w,h,&varTxtName,1)	\
	LDEF_TXTNAME(&varCon,varTxtName,&varTxtset,0,0,0,0,0,l + TXTN_L_OF, t + TXTN_T_OF,TXTN_W,TXTN_H,nameID,NULL)	\
	LDEF_TXTSET(&varCon,varTxtset,NULL/*&varLine*/,1,1,1,1,1,l + TXTS_L_OF, t + TXTS_T_OF,TXTS_W,TXTS_H,0,setstr)\
	LDEF_LINE(&varCon,varLine,NULL,1,1,1,1,1,l + LINE_L_OF, t + LINE_T_OF,LINE_W,LINE_H,0,0)

#define LDEF_MM_ITEM2(root,varCon,nxtObj,varTxtName,varTxtset,varLine,ID,IDu,IDd,l,t,w,h,nameID,setstr,pat)	\
	LDEF_CON(&root,varCon,nxtObj,ID,ID,ID,IDu,IDd,l,t,w,h,&varTxtName,1)	\
	LDEF_TXTNAME(&varCon,varTxtName,&varTxtset,0,0,0,0,0,l + TXTN_L_OF, t + TXTN_T_OF,TXTN_W,TXTN_H,nameID,NULL)	\
   	LDEF_EDIT(&varCon,varTxtset,NULL/*&varLine*/,1,1,1,1,1,l + TXTS_L_OF, t + TXTS_T_OF,TXTS_W,TXTS_H,NORMAL_EDIT_MODE , CURSOR_NORMAL, pat,NULL,NULL, setstr)\
	LDEF_LINE(&varCon,varLine,NULL,1,1,1,1,1,l + LINE_L_OF, t + LINE_T_OF,LINE_W,LINE_H,0,0)

#define LDEF_MTXT(root,varMtxt,nextObj,l,t,w,h,cnt,content)	\
	DEF_MULTITEXT(varMtxt,&root,nextObj,C_ATTR_ACTIVE,0, \
    	0,0,0,0,0, l,t,w,h, MTXT_SH_IDX,MTXT_SH_IDX,MTXT_SH_IDX,MTXT_SH_IDX,   \
	    NULL,NULL,  \
    	C_ALIGN_LEFT | C_ALIGN_TOP, cnt,  4,4,w-8,h-8,NULL,content)

#ifdef TIME_DISPLAY
LDEF_MM_ITEM(win_time_con,time_item_con1, &time_item_con3,time_item_txtname1,time_item_txtset1,time_item_line1,1,7,3,	\
				CON_L, CON_T + (CON_H + CON_GAP)*0,CON_W,CON_H,	RS_SYSTEM_TIME_REGION_NAME,NULL)
#else
LDEF_MM_ITEM(win_time_con,time_item_con1, &time_item_con3,time_item_txtname1,time_item_txtset1,time_item_line1,1,6,3,	\
				CON_L, CON_T + (CON_H + CON_GAP)*0,CON_W,CON_H,	RS_SYSTEM_TIME_REGION_NAME,NULL)
#endif
/*
LDEF_MM_ITEM(win_time_con,time_item_con2, &time_item_con3,time_item_txtname2,  time_item_txtset2,time_item_line2,2,1,3,	\
				CON_L, CON_T + (CON_H + CON_GAP)*1,CON_W,CON_H,	RS_SYSTEM_TIME_GMT_USAGE,NULL)*/
LDEF_MM_ITEM(win_time_con,time_item_con3, &time_item_con5,time_item_txtname3,time_item_txtset3,time_item_line3,3,1,5,	\
				CON_L, CON_T + (CON_H + CON_GAP)*1,CON_W,CON_H,	RS_SYSTEM_TIME_GMT_OFFSET,display_strs[0])
/*
LDEF_MM_ITEM(win_time_con,time_item_con4, &time_item_con5,time_item_txtname4,  time_item_txtset4,time_item_line4,4,3,5,	\
				CON_L, CON_T + (CON_H + CON_GAP)*3,CON_W,CON_H,	RS_SYSTEM_TIME_SUMMER_TIME,NULL)
*/
LDEF_MM_ITEM2(win_time_con,time_item_con5, &time_item_con6,time_item_txtname5,time_item_edit5,time_item_line5,5,3,6,	\
				CON_L, CON_T + (CON_H + CON_GAP)*2,CON_W,CON_H,	RS_SYSTEM_TIME_DATE,display_strs[1],win_time_pat_date)

#ifdef TIME_DISPLAY
LDEF_MM_ITEM2(win_time_con,time_item_con6, &time_item_con7,time_item_txtname6,time_item_edit6,time_item_line6,6,5,7,	\
				CON_L, CON_T + (CON_H + CON_GAP)*3,CON_W,CON_H,	RS_SYSTEM_TIME,display_strs[2],win_time_pat_time)
LDEF_MM_ITEM(win_time_con,time_item_con7, &time_mtxt_zone_city,time_item_txtname7,time_item_txtset7,time_item_line7,7,6,1,	\
				CON_L, CON_T + (CON_H + CON_GAP)*4,CON_W,CON_H,	RS_SYSTEM_TIME_DISPLAY,NULL)
#else
LDEF_MM_ITEM2(win_time_con,time_item_con6, &time_mtxt_zone_city,time_item_txtname6,time_item_edit6,time_item_line6,6,5,1,	\
				CON_L, CON_T + (CON_H + CON_GAP)*3,CON_W,CON_H,	RS_SYSTEM_TIME,display_strs[2],win_time_pat_time)
LDEF_MM_ITEM(win_time_con,time_item_con7, &time_mtxt_zone_city,time_item_txtname7,time_item_txtset7,time_item_line7,7,6,1,	\
				CON_L, CON_T + (CON_H + CON_GAP)*4,CON_W,CON_H,	RS_SYSTEM_TIME_DISPLAY,NULL)
#endif

extern TEXT_CONTENT timezone_mtxt_content[];

#if Maker_GUI_ON
LDEF_MTXT(win_time_con,time_mtxt_zone_city,NULL,\
		0,0,0,0,1,timezone_mtxt_content)
		
#else
LDEF_MTXT(win_time_con,time_mtxt_zone_city,NULL,\
		MTXT_L, MTXT_T,MTXT_W,MTXT_H,1,timezone_mtxt_content)
#endif

DEF_CONTAINER(win_time_con,NULL,NULL,C_ATTR_ACTIVE,0, \
    1,0,0,0,0, W_L,W_T,W_W,W_H, WIN_SH_IDX,WIN_HL_IDX,WIN_SL_IDX,WIN_GRY_IDX,   \
    time_con_keymap,time_con_callback,  \
    (POBJECT_HEAD)&time_item_con1, 1,0)


/*******************************************************************************
*	Local functions & variables define
*******************************************************************************/
CONTAINER* time_items[] =
{
	&time_item_con1,
//	&time_item_con2,
	&time_item_con3,
//	&time_item_con4,
	&time_item_con5,
	&time_item_con6,
	&time_item_con7,
};

POBJECT_HEAD time_item_name[] =
{
	(POBJECT_HEAD)&time_item_txtname1,
//	(POBJECT_HEAD)&time_item_txtname2,
	(POBJECT_HEAD)&time_item_txtname3,
//	(POBJECT_HEAD)&time_item_txtname4,
	(POBJECT_HEAD)&time_item_txtname5,
	(POBJECT_HEAD)&time_item_txtname6,
	(POBJECT_HEAD)&time_item_txtname7,
};

POBJECT_HEAD time_item_set[] =
{
	(POBJECT_HEAD)&time_item_txtset1,
//	(POBJECT_HEAD)&time_item_txtset2,
	(POBJECT_HEAD)&time_item_txtset3,
//	(POBJECT_HEAD)&time_item_txtset4,
	(POBJECT_HEAD)&time_item_edit5,
	(POBJECT_HEAD)&time_item_edit6,
	(POBJECT_HEAD)&time_item_txtset7,
};

date_time win_time_local_time;

TEXT_CONTENT timezone_mtxt_content[] = 
{
    {STRING_UNICODE,0},
};

UINT16 gmt_usage_strs[] =
{
	RS_SYSTEM_TIME_GMT_BYREGION,
	RS_SYSTEM_TIME_GMT_USER,
#if ((!defined(CI_PLUS_SUPPORT)) && (!defined(SUPPORT_CAS9)))
	RS_COMMON_OFF,
#endif
};

extern void win_time_set_time_zone_citys(BOOL update);


void win_time_set_Region_string(UINT8 input)
{
	OSD_SetTextFieldContent(&time_item_txtset1, STRING_ID, region_param[input].region_str);	
}

void win_time_set_GMT_enable_string(UINT8 input)
{
		//Martin@20150310
		//OSD_SetTextFieldContent(&time_item_txtset2, STRING_ID,gmt_usage_strs[input] );
}
/*
void win_time_set_Summer_string(UINT8 input)
{
	if(input==0)
		OSD_SetTextFieldContent(&time_item_txtset4, STRING_ID,RS_COMMON_OFF);				
	else
		OSD_SetTextFieldContent(&time_item_txtset4, STRING_ID,RS_COMMON_ON);	
}
*/
void win_time_set_Display_string(UINT8 input)
{
	if(input==0)
		OSD_SetTextFieldContent(&time_item_txtset7, STRING_ID,RS_COMMON_OFF);				
	else
		OSD_SetTextFieldContent(&time_item_txtset7, STRING_ID,RS_COMMON_ON);	
}

void win_time_switch_GMT_Type(UINT8 enable)
{

	enable = 0; //Martin@20150310
	if( enable == 0 ) // by region
	{
		if( !OSD_CheckAttr(&time_item_con3, C_ATTR_INACTIVE))
		{
			OSD_SetAttr(&time_item_con3, C_ATTR_INACTIVE);
			OSD_SetAttr(&time_item_txtname3, C_ATTR_INACTIVE);
			OSD_SetAttr(&time_item_txtset3, C_ATTR_INACTIVE);
		}
/*
		if( !OSD_CheckAttr(&time_item_con4, C_ATTR_INACTIVE))
		{
			OSD_SetAttr(&time_item_con4, C_ATTR_INACTIVE);
			OSD_SetAttr(&time_item_txtname4, C_ATTR_INACTIVE);
			OSD_SetAttr(&time_item_txtset4, C_ATTR_INACTIVE);
		}
*/
		if( !OSD_CheckAttr(&time_item_con5, C_ATTR_INACTIVE))
		{
			OSD_SetAttr(&time_item_con5, C_ATTR_INACTIVE);
			OSD_SetAttr(&time_item_txtname5, C_ATTR_INACTIVE);
			OSD_SetAttr(&time_item_edit5, C_ATTR_INACTIVE);
		}
		if( !OSD_CheckAttr(&time_item_con6, C_ATTR_INACTIVE))
		{
			OSD_SetAttr(&time_item_con6, C_ATTR_INACTIVE);
			OSD_SetAttr(&time_item_txtname6, C_ATTR_INACTIVE);
			OSD_SetAttr(&time_item_edit6, C_ATTR_INACTIVE);
		}
	}
	else if( enable == 1 ) // user define
	{
		if( !OSD_CheckAttr(&time_item_con3, C_ATTR_ACTIVE))
		{
			OSD_SetAttr(&time_item_con3, C_ATTR_ACTIVE);
			OSD_SetAttr(&time_item_txtname3, C_ATTR_ACTIVE);
			OSD_SetAttr(&time_item_txtset3, C_ATTR_ACTIVE);
		}
/*
		if( !OSD_CheckAttr(&time_item_con4, C_ATTR_ACTIVE))
		{
			OSD_SetAttr(&time_item_con4, C_ATTR_ACTIVE);
			OSD_SetAttr(&time_item_txtname4, C_ATTR_ACTIVE);
			OSD_SetAttr(&time_item_txtset4, C_ATTR_ACTIVE);
		}
*/
		if( !OSD_CheckAttr(&time_item_con5, C_ATTR_INACTIVE))
		{
			OSD_SetAttr(&time_item_con5, C_ATTR_INACTIVE);
			OSD_SetAttr(&time_item_txtname5, C_ATTR_INACTIVE);
			OSD_SetAttr(&time_item_edit5, C_ATTR_INACTIVE);
		}
		if( !OSD_CheckAttr(&time_item_con6, C_ATTR_INACTIVE))
		{
			OSD_SetAttr(&time_item_con6, C_ATTR_INACTIVE);
			OSD_SetAttr(&time_item_txtname6, C_ATTR_INACTIVE);
			OSD_SetAttr(&time_item_edit6, C_ATTR_INACTIVE);
		}
	}
	else // gmt off
	{
		if( !OSD_CheckAttr(&time_item_con3, C_ATTR_INACTIVE))
		{
			OSD_SetAttr(&time_item_con3, C_ATTR_INACTIVE);
			OSD_SetAttr(&time_item_txtname3, C_ATTR_INACTIVE);
			OSD_SetAttr(&time_item_txtset3, C_ATTR_INACTIVE);
		}
/*
		if( !OSD_CheckAttr(&time_item_con4, C_ATTR_INACTIVE))
		{
			OSD_SetAttr(&time_item_con4, C_ATTR_INACTIVE);
			OSD_SetAttr(&time_item_txtname4, C_ATTR_INACTIVE);
			OSD_SetAttr(&time_item_txtset4, C_ATTR_INACTIVE);
		}
*/
		if( !OSD_CheckAttr(&time_item_con5, C_ATTR_ACTIVE))
		{
			OSD_SetAttr(&time_item_con5, C_ATTR_ACTIVE);
			OSD_SetAttr(&time_item_txtname5, C_ATTR_ACTIVE);
			OSD_SetAttr(&time_item_edit5, C_ATTR_ACTIVE);
		}
		if( !OSD_CheckAttr(&time_item_con6, C_ATTR_ACTIVE))
		{
			OSD_SetAttr(&time_item_con6, C_ATTR_ACTIVE);
			OSD_SetAttr(&time_item_txtname6, C_ATTR_ACTIVE);
			OSD_SetAttr(&time_item_edit6, C_ATTR_ACTIVE);
		}
	}
}

void win_time_set_GMT_Offset_string(UINT8 input)
{
	UINT8 hour,minute,offset;
	UINT8 temp[15];

	if(input>=23)
	{
		offset=input-23;
		hour=offset/2;
		minute=offset%2 * 30;
		sprintf(temp, "UTC + %02d:%02d", hour, minute);
	}
	else
	{
		offset=23-input;
		hour=offset/2;
		minute=offset%2 * 30;
		sprintf(temp, "UTC - %02d:%02d", hour, minute);
	}
	
	ComAscStr2Uni(temp, display_strs[0]);
}

void win_time_set_time_zone_citys(BOOL update)
{
	//POBJECT_HEAD pObj =  (POBJECT_HEAD)&time_item_con3;
	MULTI_TEXT* mtxt = &time_mtxt_zone_city;
	SYSTEM_DATA* p_sys_data=sys_data_get();

	TEXT_CONTENT* txtcont = timezone_mtxt_content;
	
	UINT8 bAction;
	struct time_zone_name* time_zone;
	INT32 i,hoffset,moffset,str_len;

	txtcont->bTextType = STRING_UNICODE;
	txtcont->text.pString = len_display_str;

	//if(OSD_CheckAttr(pObj, C_ATTR_INACTIVE))
   #if ((!defined(CI_PLUS_SUPPORT)) && (!defined(SUPPORT_CAS9)))
	if( p_sys_data->gmt_select == 2 )
  #else
	if( p_sys_data->gmt_select == 1 )
  #endif
		ComAscStr2Uni("", len_display_str);		
	else
	{
		sys_data_gmtoffset_2_hmoffset(&hoffset,&moffset);
		time_zone = get_time_zone_name(hoffset, moffset);
#if Maker_GUI_ON
			ComAscStr2Uni("", len_display_str);	
#else
		if(time_zone == NULL)
			ComAscStr2Uni("", len_display_str);	
		else
		{
			str_len = 0;
			for(i=0;i<time_zone->namecnt;i++)
			{
				if(i!=0)
				{
					str_len = ComUniStrLen(len_display_str);
					SetUniStrCharAt(len_display_str,',', str_len);
					str_len++;
				}
				ComAscStr2Uni(time_zone->city_name[i], &len_display_str[str_len]);
			}			
		}
#endif		
	}

	if(update)
		OSD_DrawObject((POBJECT_HEAD)mtxt, C_UPDATE_ALL);

}

void win_time_get_input()
{
	OSD_GetEditFieldTimeDate(&time_item_edit5,&win_time_local_time);
	OSD_GetEditFieldTimeDate(&time_item_edit6,&win_time_local_time);
}

UINT8 check_is_valid_mon_day_year()
{
    UINT8 mm, is_leap_year=0;
    UINT8 dd;
    UINT16 year;

    year=win_time_local_time.year;
    mm=win_time_local_time.month;
    dd=win_time_local_time.day;
    if(!(year%4)&&(year%100) || !(year%400))
        is_leap_year = 1;
    
    if(year<1901 || year>2099)
        return 0;
    if(mm<1 || mm>12)
        return 0;
    if((mm == 1||mm == 3||mm == 5||mm == 7||mm == 8||mm == 10||mm == 12) && (dd<1 || dd>31))
        return 0;
    if((mm == 4||mm == 6||mm == 9||mm == 11) && (dd<1 || dd>30))
        return 0;
    if((is_leap_year == 1) &&(mm == 2) && (dd<1 || dd>29)) 
        return 0;
    if((is_leap_year == 0) &&(mm == 2) && (dd<1 || dd>28)) 
        return 0;

    return 1;
}


UINT8 check_is_valid_hh_mm()
{
    UINT8 hh;
    UINT8 mm;

    hh=win_time_local_time.hour;
    mm=win_time_local_time.min;
    if(mm>=60 || hh>23)
        return 0;
    else
        return 1;
}

void set_time_data(void)
{
	OSD_SetEditFieldContent(&time_item_edit5, STRING_NUMBER, win_time_local_time.year*10000+win_time_local_time.month*100+win_time_local_time.day);
}

void set_time_clock(void)
{
	OSD_SetEditFieldContent(&time_item_edit6, STRING_NUMBER, win_time_local_time.hour*100+win_time_local_time.min);
}

VACTION win_time_edit_keymap(POBJECT_HEAD pObj, UINT32 Key)
{
	VACTION Action = VACT_PASS;

	switch(Key)
	{
		case V_KEY_LEFT:
			Action = VACT_EDIT_LEFT;
			break;
		case V_KEY_RIGHT:
			Action = VACT_EDIT_RIGHT;
			break;
		case V_KEY_0:	case V_KEY_1:	case V_KEY_2:	case V_KEY_3:
		case V_KEY_4:	case V_KEY_5:	case V_KEY_6:	case V_KEY_7:
		case V_KEY_8:	case V_KEY_9:
			Action = Key - V_KEY_0 + VACT_NUM_0;
			break;
		default:
			break;
	}
	
    return Action;
}

/*******************************************************************************
*	key mapping and event callback definition
*******************************************************************************/

static VACTION time_item_con_keymap(POBJECT_HEAD pObj, UINT32 key)
{
	VACTION act;
	
	switch(key)
	{
	case V_KEY_RIGHT:
		act = VACT_INCREASE;
		break;		
	case V_KEY_LEFT:		
		act = VACT_DECREASE;
		break;		
	case V_KEY_ENTER:
		act = VACT_ENTER;
		break;		
	default:	
		act = VACT_PASS;
		break;
	}

	return act;

}

UINT8 win_time_get_num()
{
	UINT8 ID=OSD_GetFocusID((POBJECT_HEAD)&win_time_con);
	SYSTEM_DATA* p_sys_data=sys_data_get();
	UINT8 num=0;
	
	switch(ID)
	{
		case MENU_TIMESET_REGION_SELECT:
			num = REGION_CNT;
			break;
		case MENU_TIMESET_GMT_ENABLE:
			//num = 3;
            num = ARRAY_SIZE(gmt_usage_strs);
			break;
		case MENU_TIMESET_GMT_OFFESET:
			num=48;
			break;
	}
	return num;
}

PRESULT comlist_menu_time_callback(POBJECT_HEAD pObj, VEVENT event, UINT32 param1, UINT32 param2)
{
	UINT16 i,wTop;
	UINT8 id;
	char str_len;
    OBJLIST* ol;
	UINT8 temp[15];
	UINT8 hour,minute,offset;
	PRESULT cbRet = PROC_PASS;

    ol = (OBJLIST*)pObj;
	
	if(event==EVN_PRE_DRAW)
	{
		id=OSD_GetFocusID((POBJECT_HEAD)&win_time_con);
		switch(id)
		{
			case MENU_TIMESET_REGION_SELECT:
				wTop = OSD_GetObjListTop(ol);
				for(i=0;i<ol->wDep && (i+wTop)<ol->wCount;i++)
	    		{
    				win_comlist_set_str(i + wTop,NULL, NULL, region_param[i + wTop].region_str );
	    		}

				break;
			case MENU_TIMESET_GMT_ENABLE:
				wTop = OSD_GetObjListTop(ol);
				for(i=0;i<ol->wDep && (i+wTop)<ol->wCount;i++)
	    		{
    				win_comlist_set_str(i + wTop,NULL, NULL,gmt_usage_strs[i + wTop]);
	    		}
				break;
			case MENU_TIMESET_GMT_OFFESET:
		    	wTop = OSD_GetObjListTop(ol);
		    	for(i=0;i<ol->wDep && (i+wTop)<ol->wCount;i++)
		    	{
	    			if(i+wTop>=23)
	    			{
	    				offset= (i+wTop) - 23;
	    				hour=offset/2;
	    				minute=offset%2 * 30;
	    				sprintf(temp, "UTC + %02d:%02d", hour, minute);
	    			}
	    			else
	    			{
	    				offset=23-(i+wTop);
	    				hour=offset/2;
	    				minute=offset%2 * 30;
	    				sprintf(temp, "UTC - %02d:%02d", hour, minute);
	    			}
	    			win_comlist_set_str(i + wTop,temp,NULL,0);
		    	}
				break;
		}
	}
	else if(event == EVN_POST_CHANGE)
		cbRet = PROC_LEAVE;

	return cbRet;
}

/*
 * select: region index
 * region select change will effect GMT Offset only if GMT Usage selected BY REGION
 */
static void _region_select_status( POBJECT_HEAD pObj, UINT8 select )
{
	INT32 hoffset, moffset;
	SYSTEM_DATA* p_sys_data=sys_data_get();
	UINT32 cntry_code = 0xFFFFFF;
	REGION_PARAM_T *p_region;

	//p_sys_data->region_id = select;
	p_sys_data->country= select;
	win_time_set_Region_string( select );
	if(p_sys_data->gmt_select == 0) // by region
	{
		//p_region = &region_param[ p_sys_data->region_id ];
		p_region = &region_param[ p_sys_data->country];
		p_sys_data->local_time.GMToffset_count = p_region->timezone;
		win_time_set_GMT_Offset_string(p_sys_data->local_time.GMToffset_count);
		cntry_code = p_region->country_code[0]<<16 | p_region->country_code[1]<<8 | p_region->country_code[2];
		tdt_set_country_code( cntry_code );
		OSD_TrackObject(pObj,C_UPDATE_ALL);
			
		sys_data_gmtoffset_2_hmoffset(&hoffset, &moffset);
		set_STC_offset( hoffset, moffset, 0);
		get_local_time(&win_time_local_time);
		set_time_data();
		set_time_clock();
		OSD_DrawObject((POBJECT_HEAD )&time_item_con3,C_UPDATE_ALL);
		OSD_DrawObject((POBJECT_HEAD )&time_item_con5,C_UPDATE_ALL);
		OSD_DrawObject((POBJECT_HEAD )&time_item_con6,C_UPDATE_ALL);				
	}
	if(p_sys_data->gmt_select == 1) // user define
	{
		
		p_region = &region_param[ p_sys_data->country];
		p_sys_data->local_time.GMToffset_count = p_region->timezone;
		win_time_set_GMT_Offset_string(p_sys_data->local_time.GMToffset_count);
		cntry_code = p_region->country_code[0]<<16 | p_region->country_code[1]<<8 | p_region->country_code[2];
		tdt_set_country_code( cntry_code );
		OSD_TrackObject(pObj,C_UPDATE_ALL);
			
		sys_data_gmtoffset_2_hmoffset(&hoffset, &moffset);
		set_STC_offset( hoffset, moffset, 0);
		get_local_time(&win_time_local_time);
		set_time_data();
		set_time_clock();
		OSD_DrawObject((POBJECT_HEAD )&time_item_con3,C_UPDATE_ALL);
		OSD_DrawObject((POBJECT_HEAD )&time_item_con5,C_UPDATE_ALL);
		OSD_DrawObject((POBJECT_HEAD )&time_item_con6,C_UPDATE_ALL);				
	}
	OSD_TrackObject(pObj,C_UPDATE_ALL);
	win_time_set_time_zone_citys(TRUE);
	
}

/*
 * select: 0, BY REGION; 1, USER DEFINE; 2, GMT OFF
 *       BY REGION  USER DEFINE  GMT OFF
 * --- |----------------------------------
 * TDT |    sync       sync        xx
 * TOT |    sync        xx         xx
 * above table, sync means time should sync tdt/tot, xx means do not sync tdt/tot
 */
static void _gmt_usage_status( POBJECT_HEAD pObj, UINT8 select )
{
	INT32 hoffset, moffset;
	SYSTEM_DATA* p_sys_data=sys_data_get();
	REGION_PARAM_T *p_region;
	p_sys_data->gmt_select = select;
#if ((!defined(CI_PLUS_SUPPORT)) && (!defined(SUPPORT_CAS9)))
	if( p_sys_data->gmt_select == 2 )
		p_sys_data->local_time.buseGMT = 0;
	else
		p_sys_data->local_time.buseGMT = 1;
#else
	p_sys_data->local_time.buseGMT = 1;
#endif
	win_time_set_GMT_enable_string( select );
	OSD_TrackObject(pObj,C_UPDATE_ALL);
	win_time_switch_GMT_Type( select );

	if( p_sys_data->gmt_select == 0 ) // by region
	{
		//p_region = &region_param[ p_sys_data->region_id ];
		p_region = &region_param[ p_sys_data->country];
		p_sys_data->local_time.GMToffset_count = p_region->timezone;
		win_time_set_GMT_Offset_string(p_sys_data->local_time.GMToffset_count);
		OSD_TrackObject(pObj,C_UPDATE_ALL);

		enable_time_offset_parse( );
		sys_data_gmtoffset_2_hmoffset(&hoffset, &moffset);
		set_STC_offset( hoffset, moffset, 0);
		get_local_time(&win_time_local_time);
		set_time_data();
		set_time_clock();
		OSD_DrawObject((POBJECT_HEAD )&time_item_con3,C_UPDATE_ALL);
		OSD_DrawObject((POBJECT_HEAD )&time_item_con5,C_UPDATE_ALL);
		OSD_DrawObject((POBJECT_HEAD )&time_item_con6,C_UPDATE_ALL);				
	}
	else
	{
		disable_time_offset_parse();
	}

	if(p_sys_data->local_time.buseGMT)
	{
		enable_time_parse();
		get_UTC(&win_time_local_time);
        set_STC_time(&win_time_local_time);
		set_time_data();
        set_time_clock();
	}
    else
    {
		disable_time_parse();
        p_sys_data->local_time.GMToffset_count = 23;
        win_time_set_GMT_Offset_string(p_sys_data->local_time.GMToffset_count);		 
    }
/*
	if( p_sys_data->gmt_select != 1 ) // user define
	{
        p_sys_data->local_time.SummerTime=0;
        enable_summer_time(0);                	
	    win_time_set_Summer_string(p_sys_data->local_time.SummerTime);
	}
*/
	OSD_DrawObject((POBJECT_HEAD )&time_item_con3,C_UPDATE_ALL);
//	OSD_DrawObject((POBJECT_HEAD )&time_item_con4,C_UPDATE_ALL);
	OSD_DrawObject((POBJECT_HEAD )&time_item_con5,C_UPDATE_ALL);
	OSD_DrawObject((POBJECT_HEAD )&time_item_con6,C_UPDATE_ALL);
	win_time_set_time_zone_citys(TRUE);
}

/*
 * offset_count: the result of TIME_ZONE( h, m )
 */
static void _gmt_offset_status( POBJECT_HEAD pObj, UINT8 offset_count )
{
	INT32 hoffset, moffset;
	SYSTEM_DATA* p_sys_data=sys_data_get();

	p_sys_data->local_time.GMToffset_count = offset_count;
	win_time_set_GMT_Offset_string(offset_count);
	OSD_TrackObject(pObj,C_UPDATE_ALL);
	
	sys_data_gmtoffset_2_hmoffset(&hoffset, &moffset);
	set_STC_offset( hoffset, moffset, 0);
	get_local_time(&win_time_local_time);
	set_time_data();
	set_time_clock();
	OSD_DrawObject((POBJECT_HEAD )&time_item_con5,C_UPDATE_ALL);
	OSD_DrawObject((POBJECT_HEAD )&time_item_con6,C_UPDATE_ALL);			
	
	win_time_set_time_zone_citys(TRUE);
}

void win_time_enter_key(POBJECT_HEAD pObj,UINT8 id)
{	
	  INT32 hoffset, moffset;//modify for adding welcome page when only open dvbt 2011 10 17
	SYSTEM_DATA* p_sys_data=sys_data_get();
	OSD_RECT rect;
	COM_POP_LIST_PARAM_T param;
	UINT8 cur_select;
	UINT16 contry_cur;
	
#if Maker_GUI_ON
	rect.uTop=Maker_GUI_SM_T;
	rect.uLeft=Maker_GUI_MM_L+(Maker_GUI_SM_W/2);
	rect.uHeight=300;
	rect.uWidth=(Maker_GUI_SM_W/2);
#else	
	rect.uLeft=CON_L+TXTS_L_OF;
	rect.uWidth=TXTS_W;
	rect.uHeight=MENU_TIMESET_GMT_OFFESET_POPUP_H;
#endif	
	param.selecttype = POP_LIST_SINGLESELECT;
	switch(id)
	{
		case MENU_TIMESET_REGION_SELECT:
#if Maker_GUI_ON
#else
			rect.uHeight= 300;
			rect.uTop=CON_T + (CON_H + CON_GAP)*0;
#endif			
			//param.cur=p_sys_data->region_id;
			param.cur=p_sys_data->country;
			cur_select = win_com_open_sub_list(POP_LIST_MENU_TIME,&rect,&param);
			_region_select_status( pObj, cur_select );

			break;
			
		case MENU_TIMESET_GMT_ENABLE:
#if Maker_GUI_ON
#else		
			rect.uHeight= 200;
			rect.uTop=CON_T + (CON_H + CON_GAP)*1;
#endif			
			param.cur=p_sys_data->gmt_select;
			
			cur_select = win_com_open_sub_list(POP_LIST_MENU_TIME,&rect,&param);
			_gmt_usage_status( pObj, cur_select );

			break;
		case MENU_TIMESET_GMT_OFFESET:
#if Maker_GUI_ON
#else		
			rect.uHeight = MENU_TIMESET_GMT_OFFESET_POPUP_H;
			rect.uTop=CON_T + (CON_H + CON_GAP)*2;
#endif			
			param.cur=p_sys_data->local_time.GMToffset_count;
			cur_select = win_com_open_sub_list(POP_LIST_MENU_TIME,&rect,&param);
			_gmt_offset_status( pObj, cur_select );
			
			break;
	}
}

void win_time_left_key(POBJECT_HEAD pObj,UINT8 id)
{
	SYSTEM_DATA* p_sys_data=sys_data_get();
	
	switch(id)
	{
		case MENU_TIMESET_REGION_SELECT:
			/*
			if( p_sys_data->region_id == 0 )
				p_sys_data->region_id = 15;
			else
				p_sys_data->region_id--;
			_region_select_status( pObj, p_sys_data->region_id );
			*/
			if( p_sys_data->country== 0 )
				p_sys_data->country = REGION_CNT-1;
			else
				p_sys_data->country--;
			_region_select_status( pObj, p_sys_data->country );
			break;
		case MENU_TIMESET_GMT_ENABLE:
			if( p_sys_data->gmt_select == 0 )
			{
			#if ((!defined(CI_PLUS_SUPPORT)) && (!defined(SUPPORT_CAS9)))
				p_sys_data->gmt_select = 2;
            #else
                p_sys_data->gmt_select = 1;
            #endif
			}
			else
				p_sys_data->gmt_select--;
			_gmt_usage_status( pObj, p_sys_data->gmt_select );

			break;
/*
		case MENU_TIMESET_SUMMER:
			p_sys_data->local_time.SummerTime=1-p_sys_data->local_time.SummerTime;
			win_time_set_Summer_string(p_sys_data->local_time.SummerTime);
			OSD_TrackObject(pObj,C_UPDATE_ALL);
			enable_summer_time(p_sys_data->local_time.buseGMT && p_sys_data->local_time.SummerTime);
			get_local_time(&win_time_local_time);
			set_time_data();
			set_time_clock();
			OSD_DrawObject((POBJECT_HEAD )&time_item_con5,C_UPDATE_ALL);
			OSD_DrawObject((POBJECT_HEAD )&time_item_con6,C_UPDATE_ALL);
			break;
*/
		case MENU_TIMESET_DISPLAY:
			p_sys_data->local_time.TimeDisplay=1-p_sys_data->local_time.TimeDisplay;
			win_time_set_Display_string(p_sys_data->local_time.TimeDisplay);
			OSD_TrackObject(pObj,C_UPDATE_ALL);
			break;
		case MENU_TIMESET_GMT_OFFESET:
			if(p_sys_data->local_time.GMToffset_count==0)
			{
				p_sys_data->local_time.GMToffset_count=47;
			}
			else
			{
				p_sys_data->local_time.GMToffset_count--;
			}
			_gmt_offset_status( pObj, p_sys_data->local_time.GMToffset_count );

			break;
	}
}

void win_time_right_key(POBJECT_HEAD pObj,UINT8 id)
{
	SYSTEM_DATA* p_sys_data=sys_data_get();

	switch(id)
	{
		case MENU_TIMESET_REGION_SELECT:
			/*
			if( p_sys_data->region_id == 15 )
				p_sys_data->region_id = 0;
			else
				p_sys_data->region_id++;
			_region_select_status( pObj, p_sys_data->region_id );
			*/
			if( p_sys_data->country== (REGION_CNT-1))
				p_sys_data->country = 0;
			else
				p_sys_data->country++;
			_region_select_status( pObj, p_sys_data->country );
			break;
		case MENU_TIMESET_GMT_ENABLE:
		#if ((!defined(CI_PLUS_SUPPORT)) && (!defined(SUPPORT_CAS9)))
			if( p_sys_data->gmt_select == 2 )
        #else
            if( p_sys_data->gmt_select == 1 )
        #endif
				p_sys_data->gmt_select = 0;
			else
				p_sys_data->gmt_select++;
			_gmt_usage_status( pObj, p_sys_data->gmt_select );

			break;
/*			
		case MENU_TIMESET_SUMMER:
			p_sys_data->local_time.SummerTime=1-p_sys_data->local_time.SummerTime;
			win_time_set_Summer_string(p_sys_data->local_time.SummerTime);
			OSD_TrackObject(pObj,C_UPDATE_ALL);			
			enable_summer_time(p_sys_data->local_time.buseGMT && p_sys_data->local_time.SummerTime);
			get_local_time(&win_time_local_time);
			set_time_data();
			set_time_clock();
			OSD_DrawObject((POBJECT_HEAD )&time_item_con5,C_UPDATE_ALL);
			OSD_DrawObject((POBJECT_HEAD )&time_item_con6,C_UPDATE_ALL);
			break;
*/
		case MENU_TIMESET_DISPLAY:
			p_sys_data->local_time.TimeDisplay=1-p_sys_data->local_time.TimeDisplay;
			win_time_set_Display_string(p_sys_data->local_time.TimeDisplay);
			OSD_TrackObject(pObj,C_UPDATE_ALL);
			break;
		case MENU_TIMESET_GMT_OFFESET:
			if(p_sys_data->local_time.GMToffset_count==47)
			{
				p_sys_data->local_time.GMToffset_count=0;
			}
			else
			{
				p_sys_data->local_time.GMToffset_count++;
			}
			_gmt_offset_status( pObj, p_sys_data->local_time.GMToffset_count );

			break;
	}
}

static PRESULT time_item_con_callback(POBJECT_HEAD pObj, VEVENT event, UINT32 param1, UINT32 param2)
{
	PRESULT ret = PROC_PASS;
	UINT8	unact,input;
	UINT8	id = OSD_GetObjID(pObj);
	SYSTEM_DATA* p_sys_data=sys_data_get();

	switch(event)
	{		
	case EVN_FOCUS_PRE_GET:
		break;
		
	case EVN_FOCUS_PRE_LOSE:
		break;

	case EVN_UNKNOWN_ACTION:
		unact = (VACTION)(param1>>16) ;
		if(unact == VACT_ENTER)
		{
			win_time_enter_key(pObj,id);
		}
		else if(unact == VACT_DECREASE)
		{
			win_time_left_key(pObj,id);
		}
		else if(unact == VACT_INCREASE)
		{
			win_time_right_key(pObj,id);
		}
		ret = PROC_LOOP;
		break;
	}
	return ret;

}

static VACTION time_con_keymap(POBJECT_HEAD pObj, UINT32 key)
{
	VACTION act;
	SYSTEM_DATA* p_sys_data=sys_data_get();
	date_time dt;
	INT32 hoffset, moffset;
	UINT8 save;
    INT32 ret1,ret2;
	
	switch(key)
	{
	case V_KEY_UP:
		act = VACT_CURSOR_UP;
		break;
		
	case V_KEY_DOWN:
		act = VACT_CURSOR_DOWN;
		break;		
		
	case V_KEY_EXIT:
	case V_KEY_MENU:
        act = VACT_CLOSE;
		win_time_get_input();
        if(p_sys_data->local_time.buseGMT)
        {
            //sys_data_gmtoffset_2_hmoffset(&hoffset, &moffset);
			//set_STC_offset( hoffset, moffset, 0);
			get_UTC(&dt);
			set_STC_time(&dt);
			enable_time_parse();                
        }
        else
        {
            ret1 = api_check_valid_date(win_time_local_time.year,win_time_local_time.month,win_time_local_time.day);
            if(ret1 != 0 || win_time_local_time.hour>23 || win_time_local_time.min>=60)
            {
                win_compopup_init(WIN_POPUP_TYPE_OKNO);
    			win_compopup_set_msg(NULL,NULL,RS_MSG_INVALID_INPUT_CONTINUE);
    			win_compopup_set_frame(GET_MID_L(300),GET_MID_T(100),320,200);
    			if(win_compopup_open_ext(&save) == WIN_POP_CHOICE_YES)
    				act = VACT_PASS;
    			else
    				act = VACT_CLOSE;
            }
            else
            {
                set_STC_offset(0,0,0);
    			set_STC_time(&win_time_local_time);
    			p_sys_data->local_time.GMToffset_count = 23;
    			disable_time_parse();            
            }
        }

		break;
		
	case V_KEY_ENTER:
		act = VACT_ENTER;
		break;

	default:	
		act = VACT_PASS;
		break;
	}

	return act;
}

static PRESULT time_con_callback(POBJECT_HEAD pObj, VEVENT event, UINT32 param1, UINT32 param2)
{
	PRESULT ret = PROC_PASS;
	UINT32 i;
	UINT8	unact;
	CONTAINER* cont = (CONTAINER*)pObj;
	UINT8	id = OSD_GetObjID(pObj);
	SYSTEM_DATA* p_sys_data;
	p_sys_data=sys_data_get();

	switch(event)
	{
	case EVN_PRE_OPEN:
		wincom_open_title((POBJECT_HEAD)&win_time_con,RS_SYS_LOCAL_TIMER_SET/*RS_SYSTEM_TIME*/, 0);
		//win_time_set_GMT_enable_string(p_sys_data->local_time.buseGMT);
    #if ((!defined(CI_PLUS_SUPPORT)) && (!defined(SUPPORT_CAS9)))
		if( p_sys_data->gmt_select> 2 )
    #else
        if( p_sys_data->gmt_select> 1 )
    #endif
        p_sys_data->gmt_select = p_sys_data->local_time.buseGMT;
        //p_sys_data->local_time.GMToffset_count = region_param[ p_sys_data->country].timezone;
		//win_time_set_Region_string( p_sys_data->region_id );
		win_time_set_Region_string( p_sys_data->country);
		win_time_set_GMT_enable_string( p_sys_data->gmt_select );
//		win_time_set_Summer_string(p_sys_data->local_time.SummerTime);
		win_time_set_Display_string(p_sys_data->local_time.TimeDisplay);
		win_time_set_GMT_Offset_string(p_sys_data->local_time.GMToffset_count);
		get_local_time(&win_time_local_time);
		set_time_data();
		set_time_clock();
		//win_time_switch_GMT_Type(p_sys_data->local_time.buseGMT);
		win_time_switch_GMT_Type(p_sys_data->gmt_select);
		win_time_set_time_zone_citys(FALSE);
        OSD_SetContainerFocus((CONTAINER*)pObj,1);
		break;
	
	case EVN_POST_OPEN:
		break;
		
	case EVN_PRE_CLOSE:
		OSD_GetEditFieldTimeDate(&time_item_edit5, &(p_sys_data->sys_dt));
		sys_data_save(1);
//		enable_summer_time(p_sys_data->local_time.buseGMT && p_sys_data->local_time.SummerTime);		
		/* Make OSD not flickering */
		*((UINT32*)param2) &= ~C_CLOSE_CLRBACK_FLG;	
		break;

	case EVN_POST_CLOSE:
		break;
	}
	return ret;
}
#endif


UINT8* win_time_get_country_code( UINT8 n )
{
	if( n >= sizeof(region_param)/sizeof(region_param[0]) )
		return NULL;
	return region_param[n].country_code;
}

